﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.IO;

namespace Publicador
{
    partial class Scan : Form
    {
        public Scan()
        {
            InitializeComponent();
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        public void Cargar()
        {
            string perfil = "";
            string filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\perfil\";
            foreach (string filePaths in Directory.GetFiles(filePath, "*.scan"))
            {
                perfil = Path.GetFileNameWithoutExtension(filePaths.ToString());
                comboBox1.Items.Add(perfil);
            }
            comboBox1.SelectedIndex = 0;
        }

        private void okButton_Click(object sender, EventArgs e)
        {            
            string Ruta = @"C:\Program Files (x86)\EMC Captiva\QuickScan\Profiles\";
            if (!Directory.Exists(Ruta))
            {
                Ruta = @"C:\Program Files\EMC Captiva\QuickScan\Profiles\";
                if (!Directory.Exists(Ruta))
                {
                    MessageBox.Show("No se encontro la carpeta Profiles en QuickScan");
                    this.DialogResult = DialogResult.Cancel;
                    return;
                }
            }
            Ruta += @"scan\";
            if (!Directory.Exists(Ruta))
                Directory.CreateDirectory(Ruta);
            try
            {
                Array.ForEach(Directory.GetDirectories(Ruta),
                delegate(string path) { Directory.Delete(path, true); });
                foreach (string filePaths in Directory.GetFiles(Ruta, "*"))
                {
                    File.Delete(filePaths);
                }
                string filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\perfil\";
                string Perfil = filePath + comboBox1.SelectedItem;
                Directory.CreateDirectory(Ruta + "DISPROEL_" + comboBox1.SelectedItem);
                File.Copy(Perfil + ".scan", Ruta + "DISPROEL_" + comboBox1.SelectedItem + ".QSP");
                File.Copy(Perfil + ".QSP", Ruta + @"DISPROEL_" + comboBox1.SelectedItem + @"\Predefined Job.QSP");                
            }
            catch (Exception)
            {
                this.DialogResult = DialogResult.Cancel;
                MessageBox.Show("Por favor cierre QuickScan y intente nuevamente");
            }            
        }
    }
}
